package de.revdev.streckenliste.managedbeans;

import de.revdev.streckenliste.dao.WildtypDAO;
import de.revdev.streckenliste.entities.Wildtyp;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.imageio.ImageIO;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

/**
 *
 * @author f.lindermaier
 */
@Named(value = "imageBuilderBean")
@SessionScoped
public class ImageBuilderBean implements Serializable {

    @Inject
    private WildtypDAO wtdao;
    private List<StreamedContent> images;

    /**
     * Creates a new instance of ImageBuilderBean
     */
    public ImageBuilderBean() {
    }

    @PostConstruct
    public void init() {
        images = new LinkedList<>();
        for (Wildtyp wt : wtdao.findAll()) {
            if (wt.isAbschPlPfl()) {
                images.add(createImage(wt.getNameWT()));
            }
        }
    }

    public StreamedContent createImage(String text) {
        try {
            BufferedImage img = new BufferedImage(100, 20, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = img.createGraphics();
//                g.setColor(new Color(0, 0, 0, 0));
//                g.drawRect(0, 0, 70, 20);
//            g.setColor(Color.WHITE);
            g.drawString("Hallo", 0, 10);
//            g.rotate(90);
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(img, "png", os);
            return new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()), "image/png");
        } catch (IOException ioe) {
            System.out.println("Fehler");
            return null;
        }
    }

    public List<StreamedContent> getImages() {
        return images;
    }

    public void setImages(List<StreamedContent> images) {
        this.images = images;
    }
}
